package com.erhuogo.action;

import java.util.List;

import org.apache.struts2.ServletActionContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.erhuogo.bean.GridPageBean;
import com.erhuogo.bean.Response;
import com.erhuogo.bean.TwoThreeCategory;
import com.erhuogo.entity.TwoCategory;
import com.erhuogo.enumerate.Result;
import com.erhuogo.service.TwoCategoryService;
import com.opensymphony.xwork2.ActionSupport;

/**
 * 二级分类Action
 * @author aibinxiao
 * @date 2016年10月26日 上午11:36:36
 */
public class TwoCategoryAction extends ActionSupport {
	private static final long serialVersionUID = -5257544152484661899L;
	private static Logger logger = LoggerFactory.getLogger(TwoCategoryAction.class);
	
	// 模型驱动使用的对象
	private TwoCategory twoCategory;

	public TwoCategory getTwoCategory() {
		return twoCategory;
	}

	public void setTwoCategory(TwoCategory twoCategory) {
		this.twoCategory = twoCategory;
	}
	
	// 注入TwoCategoryService
	private TwoCategoryService twoCategoryService;

	public TwoCategoryService getTwoCategoryService() {
		return twoCategoryService;
	}

	public void setTwoCategoryService(TwoCategoryService twoCategoryService) {
		this.twoCategoryService = twoCategoryService;
	}
	
	// 根据一级商品分类的Id
	private Integer oneCategoryId;
	
	public Integer getOneCategoryId() {
		return oneCategoryId;
	}

	public void setOneCategoryId(Integer oneCategoryId) {
		this.oneCategoryId = oneCategoryId;
	}
	
	// 返回页面数据
	private Response response = new Response();

	public Response getResponse() {
		return response;
	}

	public void setResponse(Response response) {
		this.response = response;
	}

	/**
	 * 1.根据一级商品分类的Id获取所有的二级、三级分类
	 * @return
	 */
	public String getTwoThreeCategory(){
		List<TwoThreeCategory> listTwoThreeCategory = twoCategoryService.findTwoThreeCategory(oneCategoryId);
		if (listTwoThreeCategory != null && listTwoThreeCategory.size()>0){
			response.setResult(Result.SUCCESS.toString());
			response.setMessage("获取二级三级商品分类列表成功");
			response.setData(listTwoThreeCategory);
		} else{
			response.setResult(Result.FAILED.toString());
			response.setMessage("获取二级三级商品分类列表失败/列表为空");
		}
		return "allTwoThreeCategory";
	}
	
	/**
	 * 2.根据一级商品分类的Id获取所有的二级分类
	 * @return
	 */
	public String getTwoCateByOneId(){
		List<TwoCategory> listTwoCategory = twoCategoryService.findTwoCateByOneId(oneCategoryId);
		if (listTwoCategory != null && listTwoCategory.size()>0){
			response.setResult(Result.SUCCESS.toString());
			response.setMessage("获取二级商品分类列表成功");
			response.setData(listTwoCategory);
		} else{
			response.setResult(Result.FAILED.toString());
			response.setMessage("获取二级商品分类列表失败/列表为空");
		}
		return "findTwoCateByOneId";
	}
	
	/**
	 * 3.分页条件查询二级分类
	 * @return
	 */
	public String listPage(){
		String page = ServletActionContext.getRequest().getParameter("page");
		String rows = ServletActionContext.getRequest().getParameter("rows");
		String id = ServletActionContext.getRequest().getParameter("id");
		String name = ServletActionContext.getRequest().getParameter("name");
		String code = ServletActionContext.getRequest().getParameter("code");
		String startDate = ServletActionContext.getRequest().getParameter("startDate");
		String endDate = ServletActionContext.getRequest().getParameter("endDate");
		String oneCategoryId = ServletActionContext.getRequest().getParameter("oneCategoryId");
		GridPageBean<TwoCategory> listPage = twoCategoryService.findByPage(page, rows, id, name, code ,startDate, endDate,oneCategoryId);
		if(listPage!=null && listPage.getRecords()>0){
			response.setResult(Result.SUCCESS.toString());
			response.setMessage("查询成功");
		}else{
			response.setResult(Result.FAILED.toString());
			response.setMessage("查询失败");
		}
		response.setData(listPage);
		return "listPageSUCCESS";
	}
	
	/**
	 * 4.根据id查询三级分类
	 * @return
	 */
	public String findById(){
		String id = ServletActionContext.getRequest().getParameter("id");
		TwoCategory twoCategory = twoCategoryService.findById(id);
		if(twoCategory!=null){
			response.setData(twoCategory);
			response.setResult(Result.SUCCESS.toString());
			response.setMessage("查询成功");
		}else{
			response.setResult(Result.FAILED.toString());
			response.setMessage("查询失败");
		}
		return "findByIdSUCCESS";
	}
	
	/**
	 * 5.保存或者更新三级分类信息
	 * @return
	 */
	public String saveTwoCategory(){
		String id = ServletActionContext.getRequest().getParameter("id");
		String code = ServletActionContext.getRequest().getParameter("code");
		String name = ServletActionContext.getRequest().getParameter("name");
		String desc = ServletActionContext.getRequest().getParameter("desc");
		String oneCategoryId = ServletActionContext.getRequest().getParameter("oneCategoryId");
		try {
			twoCategoryService.save(id,code,name,desc,oneCategoryId);
			response.setResult(Result.SUCCESS.toString());
		} catch (Exception e) {
			response.setResult(Result.FAILED.toString());
			e.printStackTrace();
			logger.info(e.getMessage());
		}
		return "saveSUCCESS";
	}
	
	/**
	 * 6.开启或者屏蔽二级分类信息
	 * @return
	 */
	public String updateStatusById(){
		String id = ServletActionContext.getRequest().getParameter("id");
		String status = ServletActionContext.getRequest().getParameter("status");
		try {
			twoCategoryService.updateStatus(id,status);
			response.setResult(Result.SUCCESS.toString());
			response.setMessage("更新状态成功");
		} catch (Exception e) {
			response.setResult(Result.FAILED.toString());
			response.setMessage("更新状态失败");
			e.printStackTrace();
			logger.info(e.getMessage());
		}
		return "updateStatusSUCCESS";
	}
	
}
